﻿import flash.filters.BlurFilter;
import mx.effects.Tween;


/**
Blurs and fades a movieclip over time
<code>
	// example syntax
	var b:BlurTween = new BlurTween(target);
	b.doBlur(blurFrom,blurTo,alphaFrom,alphaTo,speed);
</code>
*/

class com.vitaly.BlurTween {
	
	private var target:MovieClip;
	
	/**
	Callback function triggered when blur has completed
	*/
	
	public var onComplete:Function;
	
	/**
	@param target the MovieClip you want to target
	*/
	
	function BlurTween (t:MovieClip) {
		
		target = t;
		
		
	}
	
	/**
	@param blurFrom the start amount of blur
	@param blurTo the end amount of blur
	@param alphaFrom the start amount of alpha
	@param alphaTo the end amount of alpha
	@param speed the speed of the transition is milliseconds
	*/
	
public function doBlur(blurFrom:Number,blurTo:Number,alphaFrom:Number,alphaTo:Number,speed:Number) {
	var t:MovieClip = target;
	var bx:Number;
	var by:Number;
	var alpha:Number;
	var pointer:Object = this;
	if (t.blurFrom != undefined) {
		blurFrom = t.blurFrom;
		alphaFrom = t._alpha;
	}
	var oTween:Tween = new Tween(t, [blurFrom,alphaFrom], [blurTo,alphaTo], speed);
	t.onTweenUpdate = function(a:Array):Void  {
		by = bx = a[0];
		alpha = a[1];
		t.blurFrom = a[0];
		pointer.blurMe(bx,by);
		pointer.alphaMe(alpha);
	}
	
	t.onTweenEnd = function() {
		
		pointer.onComplete();
	}
	
	
	
}
private function blurMe(bx:Number,by:Number) {
			var filter:BlurFilter = new BlurFilter(bx, by, 2);
			var filterArray:Array = new Array();
			filterArray.push(filter);
			target.filters = filterArray;
			
}

private function alphaMe(alpha:Number) {
	
	target._alpha = alpha;
	
}
	
}